import csv
import os
from datetime import datetime
import time
import okx.MarketData as MarketData
from utils.log import Loggers
from utils.config import *
from utils.utils import *
flag = "0"  # 实盘:0 , 模拟盘：1
marketDataAPI =  MarketData.MarketAPI(flag=flag,domain='https://www.chouyi.army')
logger = Loggers('downloadK')



# 读取文件 并对文件进行 处理，转为方便查看的  时间日期格式
def sort_csv_by_timestamp(instId,bar='15m'):
    filename = getFilename(instId,bar)
    """读取CSV文件并按时间戳排序，同时去重，只保留指定列"""
    required_columns = ["timestamp", "open", "high", "low", "close", "volume"]
    
    with open(filename, mode='r', newline='', encoding='utf-8') as file:
        reader = csv.DictReader(file)
        rows = list(reader)
        
        # 按时间戳排序
        rows.sort(key=lambda x: int(x['timestamp']))
        
        # 去重：保留每个时间戳的第一个记录，并只保留指定列
        unique_rows = {}
        for row in rows:
            timestamp = int(row['timestamp'])
            if timestamp not in unique_rows:
                # 只保留所需的列
                unique_rows[timestamp] = {
                    "timestamp": timeToDate(row['timestamp']),
                    "open": row['open'],
                    "high": row['high'],
                    "low": row['low'],
                    "close": row['close'],
                    "volume": row['volume']
                }
        
        # 将去重后的记录转换为列表
        unique_rows_list = list(unique_rows.values())
 
    with open(filename.replace('/data/',f'/data_look/'), mode='w', newline='', encoding='utf-8') as file:
        writer = csv.DictWriter(file, fieldnames=required_columns)
        writer.writeheader()
        writer.writerows(unique_rows_list)


# 如果文件夹 ./data 不存在 则创建
if not os.path.exists("./data_look"):
    os.makedirs("./data_look")

# 对数据进行清洗 并保存到可读文件中
instId  = 'BTC'
sort_csv_by_timestamp(instId+'-USDT-SWAP','15m')
print('数据转换完成')